package com.zxl.yjb.base;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * A simple {@link Fragment} subclass.
 */
public abstract class BaseFragment extends Fragment {

    protected BaseActivity mActivity;
    private Unbinder unbinder;

    /**
     * 获取Fragment的布局文件
     * @return
     */
    protected abstract int getLayoutId();


    /**
     * 初始化布局
     * @param view
     * @param savedInstanceState
     */
    protected abstract void initView(View view, Bundle savedInstanceState);

    /**
     * Fragment运行入口
     */
    protected abstract void main();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mActivity= (BaseActivity) context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(getLayoutId(),container,false);
        unbinder = ButterKnife.bind(this, view);
        initView(view,savedInstanceState);
        main();
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null)
            unbinder.unbind();
    }
}
